package com.sf.web.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.sf.data.MyDataSource;
import com.sf.service.IAmountService;
import com.sf.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.DataSource;
import java.math.BigDecimal;

@Controller
public class HelloController {

    @Autowired
    private IAmountService amountService;

    /**
     * 接口:
     * 访问/hello
     * 返回{success: true,msg: 操作成功,data:null}
     * JsonResult(success,msg,data)
     */
    @RequestMapping("/hello")
    @ResponseBody
    public JsonResult hello(){
        int i = 1/0;
        return new JsonResult(true,"操作成功",null);
    }
    /**
     * 接口:
     * 请求地址: /trans?toId=收款人Id&fromId=付款人id&amount=转账金额
     * 返回值:  {success: true/false,msg: 转账成功/转账失败,data: null}
     */
    @RequestMapping("/trans")
    @ResponseBody
    public JsonResult trans(Long toId, Long fromId, BigDecimal amount){
        try{
            amountService.trans(toId,fromId,amount);
            return new JsonResult(true,"转账成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"转账失败",null);
        }
    }
}
